package com.itheima.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mapper.UmsRoleMenuRelationMapper;
import com.itheima.pojo.UmsRoleMenuRelation;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mapper.UmsMenuMapper;
import com.itheima.mapper.UmsRoleMenuRelationMapper;
import com.itheima.pojo.UmsMenu;
import com.itheima.pojo.UmsRoleMenuRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@DubboService
public class UmsRoleMenuRelationApiImpl implements UmsRoleMenuRelationApi{

    @Autowired
    private UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;


    /**
     * 为角色分配菜单
     * @param menuIds
     * @param roleId
     */
    @Override
    public void allocMenu(Integer[] menuIds, Integer roleId) {
        // 获取角色已分配菜单
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleMenuRelation::getRoleId, roleId);
        List<UmsRoleMenuRelation> allocedMenuList = umsRoleMenuRelationMapper.selectList(queryWrapper);
        // 提取已分配菜单id列表
        List<Long> allocedMenuIdList = CollUtil.getFieldValues(allocedMenuList, "menuId", Long.class);
        // 创建要分配菜单id集合
        Set<Long> willAllocMenuIdSet = Arrays.stream(menuIds).map(Convert::toLong).collect(Collectors.toSet());
        // 做差集运算
        willAllocMenuIdSet.removeAll(allocedMenuIdList);
        // 分配
        if(willAllocMenuIdSet.size() > 0) {
            umsRoleMenuRelationMapper.allocMenu(willAllocMenuIdSet, roleId);
        }
    }


    /**
     * 获取指定角色的菜单id
     * @param roleId
     * @return
     */
    @Override
    public List<Long> getRoleRelatedMenuId(Integer roleId) {
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleMenuRelation::getRoleId, roleId);
        List<UmsRoleMenuRelation> roleMenuRelationList = umsRoleMenuRelationMapper.selectList(queryWrapper);
        List<Long> menuId = CollUtil.getFieldValues(roleMenuRelationList, "menuId", Long.class);
        return menuId;
    }


    /**
     * 根据角色id获取对应菜单id
     * @param roleIds
     * @return
     */
    @Override
    public List<UmsRoleMenuRelation> findMenuIdsByRoleIds(List<Long> roleIds) {
        if(roleIds.size() > 0) {
            LambdaQueryWrapper<UmsRoleMenuRelation> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(CollUtil.isNotEmpty(roleIds), UmsRoleMenuRelation::getRoleId, roleIds);
            return umsRoleMenuRelationMapper.selectList(wrapper);
        }else {
            return Collections.emptyList();
        }
    }


    /**
     * 根据角色id删除与目录的关联
     * @param ids
     */
    @Override
    public void deleteByRoleId(Integer[] ids) {
        if(ids.length > 0){
            LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(UmsRoleMenuRelation::getRoleId, ids);
            umsRoleMenuRelationMapper.delete(queryWrapper);
        }
    }


    /**
     * 根据目录id删除角色和目录的关联
     */
    @Override
    public void deleteByMenuId(List<Long> idList) {
        if(idList.size() > 0) {
            LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(UmsRoleMenuRelation::getMenuId, idList);
            umsRoleMenuRelationMapper.delete(queryWrapper);
        }
    }
}
